const {ccclass, property} = cc._decorator;

@ccclass
export default class sound extends cc.Component {
    static base_sound_url:string = 'sounds/';
    static sound_name = new Array();
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    public static async preload(){
        orc.log('sound preload'); 
        return new Promise((resolve,reject)=>{
            cc.loader.loadResDir(this.base_sound_url,cc.AudioClip,(err, audioClip) => {                
                for(let i=0;i<audioClip.length;i++)
                {                
                    this.sound_name[audioClip[i].name] = audioClip[i];
                }    
                resolve('ok');            
            });
        });
    }
   
    /**
     * 播放音乐
     * @param url: 播放地址
     * @param loop: 是否循环
    */
    public static play_music(name,loop:boolean)
    {
        if(this.sound_name[name])
        {
            cc.audioEngine.playMusic(this.sound_name[name],loop)
        }   
        else
        {
            orc.log('sound name noexist');
        }
        
    }

     /**
     * 停止音乐    
     */
    public static stop_music(){
        cc.audioEngine.stopMusic();
    }

    /**
     * 播放音效
     * @param url: 播放地址
    */
    public static play_effect(name){       
        if(this.sound_name[name])
        {
            cc.audioEngine.playEffect(this.sound_name[name],false);
        }   
        else
        {
            orc.log('sound name noexist');
        }     
    }

    // update (dt) {}
}
